
package org.yumeko.cmdb.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class CmdbUtil {

    public static Map<String, Object> generateParams(CmdbProperties cmdbProperties, String modelName , int page) {
        // API的参数
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("model_name", modelName);
//        apiParams.put("relation", true);
        apiParams.put("page", page);
        apiParams.put("pageSize", cmdbProperties.getPageSize());
        Map<String, Object> params = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String dataJson = objectMapper.writeValueAsString(apiParams);
            params.put("data", dataJson);
            params.put("timestamp", System.currentTimeMillis() / 1000);
            // secret + 参数进行连接，得到加密前的字符串
            String hashStr = String.format("%sdata%stimestamp%d", cmdbProperties.getSecret(), dataJson, params.get("timestamp"));
            // 对上述字符串进行sha1算法加密
            String sign = DigestUtils.sha1Hex(hashStr);
            params.put("sign", sign);
        } catch (Exception e) {
            log.error("请求异常: {}", e.getMessage(), e);
        }

        return params;
    }
}